package com.rpframework.website.device.act.api;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.act.admin.AdminAct;
import com.rpframework.website.device.domain.Goods;
import com.rpframework.website.device.domain.ShoppingCart;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.GoodsService;
import com.rpframework.website.device.service.ShoppingCartService;

@Controller
@RequestMapping("api/shoppingcart")
public class ApiShoppingCartAct extends AdminAct{
	Gson gson =new Gson();
	@Resource ShoppingCartService service;
	@Resource GoodsService goodsService;
	/**
	 * 当前登录用户的购物车
	 * @param pager
	 * @param callback
	 * @param response
	 * @param session
	 * @return
	 * @time 2016年3月10日 上午11:06:04
	 */
	@RequestMapping(value="/cartList",produces = "application/json; charset=utf-8")
	public @ResponseBody String listGoods(@RequestParam(value="pager",required = false ) Pager<Goods> pager,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpServletResponse response,
			HttpSession session){
		User user = getSessionUser(session);
		if(user == null){
			throw new APICodeException(-4, "你还没登陆!");
		}	
		List<ShoppingCart> list = service.getCartGoodsByUser(user.getId());
		JsonObject json = new JsonObject();
		JsonArray array=new JsonArray();
		json.add("arrays", array);
		for(ShoppingCart act : list){
			JsonObject jsonObj = new JsonObject();
			jsonObj.addProperty("id", act.getId());
			jsonObj.addProperty("goodId", act.getGoodsid());
			jsonObj.addProperty("goodName", act.getGoods().getName());
			JsonArray arr = new JsonParser().parse(act.getGoods().getUrlList()).getAsJsonArray();
			jsonObj.add("urlList", arr);//商品图片
			jsonObj.addProperty("price", act.getGoods().getDisPrice()==0 ? act.getGoods().getOldPrice() : act.getGoods().getDisPrice());
			jsonObj.addProperty("goodsNum", act.getGoodsNum());
			array.add(jsonObj);
		}
		return (callback + "(" + json + ")");
	}
	
	//添加购物车
	/**
	 * 添加
	 * 添加多个id数组或以，分隔的id
	 * @param goodsid
	 * @param callback
	 * @param session
	 * @return
	 * @time 2016年3月8日 下午8:27:09
	 */
	@RequestMapping(value="/addcart",produces = "application/json; charset=utf-8")
	public  @ResponseBody String add(
			@RequestParam(required=false) Integer goodsid,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		User currUser = getSessionUser(session);	
		if(currUser == null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		Goods good = goodsService.select(goodsid);
		ShoppingCart cart = new ShoppingCart();
		cart.setGoodsid(goodsid.toString());
		cart.setUserid(currUser.getId().toString());
		cart.setGoods(good);
		cart.setUser(currUser);
		cart.setNumber(1);
		boolean blfg=service.insert(cart);
		JsonObject json=new JsonObject();
		if(blfg == true){ // 修改成功
			json.addProperty("succ", true);
		} else { // 修改失败
			json.addProperty("error", false);
		} 
		return (callback + "(" + json + ")");
	}
	
	@RequestMapping(value="/commitCart",produces = "application/json; charset=utf-8")
	public @ResponseBody String commitCart(
			@RequestParam(value = "callback",required = false ) String callback,
			@RequestParam(value = "ids",required = false ) List<Integer> ids,
			@RequestParam(value = "amount",required = false ) List<Integer> amount,
			HttpSession session){
		User sessionuser=getSessionUser(session);
		if(sessionuser==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		JsonObject json=new JsonObject();
		//将这些商品和数量生成订单
		
		//Dityorder t = new Dityorder();
		
		service.doCleanCart(ids);
		//将这些商品id从购物车里移除掉
		json.addProperty("succ", true);
		return (callback + "(" + json + ")");
	}
	@RequestMapping(value="/delCart",produces = "application/json; charset=utf-8")
	public @ResponseBody String delCart(
			@RequestParam(value = "callback",required = false ) String callback,
			@RequestParam(value = "ids",required = false ) List<Integer> ids,
			HttpSession session){
		User sessionuser=getSessionUser(session);
		if(sessionuser==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		JsonObject json=new JsonObject();
		//将这些商品id从购物车里移除掉
		service.doCleanCart(ids);
		json.addProperty("succ", true);
		return (callback + "(" + json + ")");
	}
	//批量删除购物车
	@RequestMapping(value="/delectCartAll",produces = "application/json; charset=utf-8")
	public @ResponseBody String delectcartAll(@RequestParam(value = "callback",required = false ) String callback,HttpSession session){
		User sessionuser=getSessionUser(session);
		if(sessionuser==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		List<ShoppingCart> list=service.selectmycart(sessionuser.getId());
		JsonObject json=new JsonObject();
		for(ShoppingCart act:list){
			service.delete(act.getId());
		}
		json.addProperty("succ", true);
		return (callback + "(" + json + ")");
	}
	//删除单个购物车
		@RequestMapping(value="/delectCartOne",produces = "application/json; charset=utf-8")
		public @ResponseBody String delectCartOne(
				@RequestParam(value = "callback",required = false ) String callback,
				@RequestParam(required=false) Integer cartid,
				HttpSession session){
			User sessionuser=getSessionUser(session);
			if(sessionuser==null){
				throw new APICodeException(-4, "你还没登陆!");
			}
			boolean bfgl = service.delete(cartid);
			JsonObject json=new JsonObject();
			if(bfgl){
				json.addProperty("succ", true);
			}else{
				json.addProperty("succ", true);
			}
			return (callback + "(" + json + ")");
		}
}
